# coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from public.common.globalconfig import *


class SendMail:
    def __init__(self, send_msg,
                 smtpserver="smtp.qq.com",
                 sender="619798853@qq.com",
                 psw="pmaxarbhcolwbbde", receiver="link@whispark.com",
                 # psw是我们授权码，不是我们的登录密码
                 port=465, attachment=None):
        # self.reportfile = reportfile
        self.send_msg = send_msg
        self.smtpserver = smtpserver
        self.sender = sender
        self.psw = psw
        self.receiver = receiver
        self.port = port
        self.attachment = attachment

    def send_mail(self):
        """发送最新的测试报告内容"""
        # 获取测试报告截图
        options = webdriver.ChromeOptions()
        options.add_argument('--headless')
        options.add_argument('--window-size=1920,1080')
        self.driver = webdriver.Chrome(options=options)
        self.driver.get(self.send_msg)
        self.driver.maximize_window()
        imgname = os.path.join(imgPath, '测试报告截图.png')
        self.driver.save_screenshot(imgname)
        self.driver.quit()
        # 打开测试报告，读取测试报告内容
        with open(self.send_msg, "rb") as f:
            mail_boday = f.read()

        with open(imgname, "rb") as f:
            mail_img = f.read()

        # 定义邮件
        msg = MIMEMultipart()
        msg['subject'] = Header("自动化测试报告", 'utf-8')
        msg['From'] = Header(self.sender, 'utf-8')
        msg['To'] = self.receiver
        # 定义邮件正文html格式
        html = """
            <html> 
              <head></head> 
              <body> 
                  <p>
                   <br><img src="cid:image1"></br> 
                  </p>
              </body> 
            </html> 
            """
        htm = MIMEText(html, 'html', 'utf-8')
        msg.attach(htm)

        # 添加附件
        if self.attachment is not None:
            file_name = self.attachment.split(os.path.sep)[-1]
            att = MIMEText(
                open(
                    self.attachment,
                    "rb").read(),
                "base64",
                "utf-8")
            att["Content-Type"] = "application/octet-stream"
            att.add_header(
                'Content-Disposition',
                'attachment',
                filename=file_name)
            msg.attach(att)
        try:
            # body = MIMEText(mail_boday, _subtype="html", _charset='utf-8')
            # msg.attach(body)
            img = MIMEImage(mail_img)
            img.add_header('Content-ID', '<image1>')
            msg.attach(img)

            smtp = smtplib.SMTP_SSL(self.smtpserver)
            # smtp.set_debuglevel(1)
            smtp.connect(self.smtpserver, self.port)
            # smtp.starttls()
            # 用户登录并发送邮件
            smtp.login(self.sender, self.psw)
            smtp.sendmail(self.sender, self.receiver, msg.as_string())
            smtp.quit()
            print('邮件发送成功')
        except BaseException:
            print('邮件发送失败')
